package com.cheer.controller;

import com.cheer.bo.SendInfoBO;
import com.cheer.common.Result;
import com.cheer.common.ResultEnum;
import com.cheer.dto.SendInfoUpdateDTO;
import com.cheer.dto.UserRegisterDTO;
import com.cheer.dto.UserUpdateDTO;
import com.cheer.service.AuthService;
import com.cheer.vo.ExMatchUserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;


/**
 * @author cheer
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @GetMapping("/login")
    public Result login(@RequestParam String code, HttpSession session) {
        authService.login(code, session);
        return Result.success();
    }

    @PostMapping("/register")
    public Result register(@RequestBody @Valid UserRegisterDTO userRegisterDTO, HttpSession session) {
        authService.register(userRegisterDTO, session);
        return Result.success();
    }

    @GetMapping("/share")
    public Result share(@RequestParam String url) {
        return Result.success(authService.share(url));
    }

    @GetMapping("/user")
    public Result<ExMatchUserVO> user(HttpSession session) {
        return Result.success(authService.user(session));
    }

    @GetMapping("/user/detail")
    public Result<ExMatchUserVO> detail(HttpSession session) {
        return Result.success(authService.info(session));
    }

    @PostMapping("/user/detail")
    public Result updateUser(@RequestBody @Valid UserUpdateDTO userUpdateDTO, HttpSession session) {
        boolean province = StringUtils.isNotBlank(userUpdateDTO.getProvince());
        boolean city = StringUtils.isNotBlank(userUpdateDTO.getCity());
        boolean region = StringUtils.isNotBlank(userUpdateDTO.getRegion());
        boolean address = StringUtils.isNotBlank(userUpdateDTO.getAddress());

        if (!((province && city && region && address) || (!province && !city && !region && !address))) {
            return Result.error(ResultEnum.SERVER_ERROR, "地址信息填写不完整");
        }

        authService.updateUser(userUpdateDTO, session);
        return Result.success();
    }

    @GetMapping("/send-info")
    public Result<SendInfoBO> getSendInfo(HttpSession session) {
        return Result.success(authService.getSendInfo(session));
    }

    @PostMapping("/send-info")
    public Result updateSendInfo(@RequestBody @Valid SendInfoUpdateDTO sendInfoUpdateDTO, HttpSession session) {
        authService.updateSendInfo(sendInfoUpdateDTO, session);
        return Result.success();
    }

}
